﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mail;

namespace KidsProtector.DataAccess
{
    public class Rate
    {
        public bool CreateRate(T_Rate rate)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                model.AddToT_Rate(rate);
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool remove(long idRate)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Rate rate = model.T_Rate.Where(x => x.Id == idRate).FirstOrDefault();
                model.DeleteObject(rate);
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public int Getnotation(long idSite, long idUser)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Rate rate = model.T_Rate.Where(x => x.IdSite == idSite && x.IdUser == idUser).FirstOrDefault();
                if (rate != null)
                    return rate.note;
                else
                {
                    Site site = new Site();
                    return site.GetMoy(idSite);
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public T_Rate GetRate(long idRate)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Rate rate = model.T_Rate.Where(x => x.Id == idRate).FirstOrDefault();
                return rate;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public T_Rate GetRate(long idSite, long idUser)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Rate rate = model.T_Rate.Where(x => x.IdSite == idSite && x.IdUser == idUser).FirstOrDefault();
                return rate;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool ModifyRate(long idRate, int note)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                T_Rate rate = model.T_Rate.Where(x => x.Id == idRate).FirstOrDefault();
                rate.note = note;
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public int GetCount(long idSite)
        {
            try
            {
                KidsProtectorEntities model = new KidsProtectorEntities();
                int count = model.T_Rate.Where(x => x.IdSite == idSite).Count();
                return count;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public void SendConfirmation(DataAccess.T_Rate rate)
        {
            BusinessManagement.Site siteBM = new BusinessManagement.Site();
            BusinessManagement.User userBM = new BusinessManagement.User();
            List<T_User> ulist = userBM.GetList();


            //     L'application n'etant pas deployee sur un serveur fixe, il est impossible de configurer l'envoi de mails.

            //if (ulist.Count > 1)
            //{
                
            //    MailMessage email = new MailMessage();
            //    email.From = "noreply@kidsprotector.com";
            //    email.To = userBM.GetMail(rate.IdUser);
            //    email.Subject = "Kids Protector - Confirmation de note";
            //    email.Body = "Bonjour,<br><br>Le site suivant " + siteBM.GetSite(rate.IdSite).Adress + " viens d'être noté!<br><br>L'utilisateur à déclaré ce site ";
            //    if (rate.note == 0)
            //        email.Body = email.Body + "Tout Publique";
            //    if (rate.note == 1)
            //        email.Body = email.Body + "Interdit au moins de 10 ans";
            //    if (rate.note == 2)
            //        email.Body = email.Body + "Interdit au moins de 16 ans";
            //    if (rate.note == 3)
            //        email.Body = email.Body + "Interdit au moins de 18 ans";
            //    email.Body = email.Body + "<br><br>Pour confirmer cette note cliquer ici : <a href='www.kidsprotector.com/Confirmation?id=" + rate.Id + "&val=yes";
            //    email.Body = email.Body + "<br><br>Pour infirmer cette note cliquer ici : <a href='www.kidsprotector.com/Confirmation?id=" + rate.Id + "&val=no";
            //    email.Body = email.Body + "<br><br>Merci de votre participation<br><br> Cordialement,<br><br>-- <br>l'équipe Kids Protector";

            //    email.Priority = MailPriority.High;
            //    SmtpMail.SmtpServer = "mon_serveur_smtp";
            //    try
            //    {
            //        SmtpMail.Send(email);
            //    }
            //    catch (Exception)
            //    {
            //    }
            //}
            //else
            //    siteBM.UpdateMoy(rate.IdSite);
            siteBM.UpdateMoy(rate.IdSite);
        }
    }
}